﻿using JESAI.Application.Abstractions.Auth;
using JESAI.Application.Abstractions.Auth.Dtos;
using JESAI.Authorize.Abstractions;
using JESAI.AutoWebApi.Setting;
using JESAI.Core.Attributes;
using JESAI.Core.Util.Extensions;
using JESAI.DDD.Services;
using System;
using System.Collections.Generic;
using System.Security.Claims;

namespace JESAI.Application.Service
{
    [AppService]
    public class AuthService : ApplicationService, IAuthService, IAutoApiService
    {
        //private readonly IConfig _config;
        private readonly ITokenBuilder _tokenBuilder;


        public AuthService(ITokenBuilder tokenBuilder)
        {
            //_config = config;
            _tokenBuilder = tokenBuilder;
        }

        /// <summary>
        /// 创建Jwt Token
        /// </summary>
        /// <param name="userInfo"></param>
        /// <param name="roles"></param>
        /// <returns></returns>
        public string CreateAccessToken(UserTokenDto userInfo, List<string> roles)
        {
            // 用户基本信息
            var claims = new List<Claim> {
                new Claim("Uid", userInfo.Id.ToString()),
                new Claim("Uids", userInfo.Ids),
                new Claim("Name", userInfo.RealName.SafeString()),
                new Claim("MobilePhone", userInfo.Mobile.SafeString()),
                new Claim("Email", userInfo.Email.SafeString())
            };
            // 角色数据
            foreach (var info in roles)
            {
                claims.Add(new Claim("scope", info));
            }
            var expires = 4;// _config.StringGet("TokenExpires", "4");
            var token = _tokenBuilder.BuildJwtToken(claims, DateTime.UtcNow.AddMinutes(-3), DateTime.Now.AddHours(Convert.ToInt32(expires)));
            // accessToken
            return token.TokenValue;
        }

        /// <summary>
        /// 获取token有效时长，单位秒
        /// </summary>
        /// <param name="houer"></param>
        /// <returns></returns>
        public double GetExpireInValue(int houer)
        {
            return new TimeSpan(houer, 0, 0).TotalSeconds;
        }
    }
}
